/*
 * TimerCallback.c
 *
 *  Created on: 22-08-2012
 *      Author: Mati
 */

#include "ApplicationTasks.h"

extern long lExpireCounter;
extern xTimerHandle xModesTimer;

enum Mode eCurrentMode = WORK;
enum PrepareState epState = STAGE1;
enum WorkState ewState = ADSORPTION;

uint16_t DesiredTemperature;

void vTimerCallback( xTimerHandle pxTimer )
{
	 /* Optionally do something if the pxTimer parameter is NULL. */
     configASSERT( pxTimer );

     /* Which timer expired? */
     //lArrayIndex = ( long ) pvTimerGetTimerID( pxTimer );
//     50,// ptemp1,
//     100,// ptemp2,
//     150,// ptemp3,
//     250,// ptemp4,
//     350,// ptemp5,
     if( 1 == PVal.autoMode)
     {
    	 if( PREPARE == eCurrentMode )
    	 {
    		 switch (epState)
    		 {
    		 	 case STAGE1:
    		 		 SetPeriodAndTemperature(50, 10);
    		 		 epState = STAGE2;
    		 		 vSetSpeakerParams(50, 500);
    		 		 break;
    		 	 case STAGE2:
    		 		 SetPeriodAndTemperature(100, 10);
    		 		 epState = STAGE3;
    		 		 vSetSpeakerParams(40, 1000);
    		 		 break;
    		 	 case STAGE3:
    		 		 SetPeriodAndTemperature(150, 10);
    		 		 epState = STAGE4;
    		 		 vSetSpeakerParams(30, 1500);
    		 		 break;
    		 	 case STAGE4:
    		 		 SetPeriodAndTemperature(250, 10);
    		 		 epState = STAGE5;
    		 		 vSetSpeakerParams(20, 2500);
    		 		 break;
    		 	 case STAGE5:
    		 		 SetPeriodAndTemperature(350, 10);
    		 		 epState = STAGE1;
    		 		 eCurrentMode = WORK;
    		 		 vSetSpeakerParams(150, 3500);
    		 		 break;
    		 }
    	 }
    	 else if( WORK == eCurrentMode)
    	 {
    		 switch(ewState)
    		 {
    		 	 case ADSORPTION:
    		 		 SetPeriodAndTemperature(250, 7);
    		 		 //PVal.dac_voltage = 3300;
    		 		 ewState = DESORPTION;
    		 		 vSetSpeakerParams(50, 500);
    		 		 break;
    		 	 case DESORPTION:
    		 		 SetPeriodAndTemperature(25, 3);
    		 		 //PVal.dac_voltage = 500;
    		 		 ewState = ADSORPTION;
    		 		 vSetSpeakerParams(50, 300);
    		 		 break;
    		 }
    	 }
     }
     else if( 1 == PVal.userMode)
          {
         	 if( PREPARE == eCurrentMode )
         	 {
         		 switch (epState)
         		 {
         		 	 case STAGE1:
         		 		 SetPeriodAndTemperature(PVal.ptemp1, PVal.ptime1);
         		 		 epState = STAGE2;
         		 		 vSetSpeakerParams(50, 500);
         		 		 break;
         		 	 case STAGE2:
         		 		 SetPeriodAndTemperature(PVal.ptemp2, PVal.ptime2);
         		 		 epState = STAGE3;
         		 		vSetSpeakerParams(40, 1000);
         		 		 break;
         		 	 case STAGE3:
         		 		 SetPeriodAndTemperature(PVal.ptemp3, PVal.ptime3);
         		 		 epState = STAGE4;
         		 		vSetSpeakerParams(30, 1500);
         		 		 break;
         		 	 case STAGE4:
         		 		 SetPeriodAndTemperature(PVal.ptemp4, PVal.ptime4);
         		 		 epState = STAGE5;
         		 		vSetSpeakerParams(20, 2500);
         		 		 break;
         		 	 case STAGE5:
         		 		 SetPeriodAndTemperature(PVal.ptemp5, PVal.ptime5);
         		 		 epState = STAGE1;
         		 		 eCurrentMode = WORK;
         		 		vSetSpeakerParams(15, 3500);
         		 		 break;
         		 }
         	 }
         	 else if( WORK == eCurrentMode)
         	 {
         		 switch(ewState)
         		 {
         		 	 case ADSORPTION:	//do sprawdzenia ten temp1 i temp2
         		 		 SetPeriodAndTemperature(PVal.wtemp1, PVal.wtime1);
         		 		 ewState = DESORPTION;
         		 		 vSetSpeakerParams(100, 500);
         		 		 break;
         		 	 case DESORPTION:
         		 		 SetPeriodAndTemperature(PVal.wtemp2, PVal.wtime2);
         		 		 ewState = ADSORPTION;
         		 		 vSetSpeakerParams(100, 300);
         		 		 break;
         		 }
         	 }
          }

     LED_Toggle(1<<4);
 }
